package study.算法Algorithm.排序算法.冒泡;

import java.util.Arrays;

/*
    冒泡排序优化
 */
public class BubbleSort2 {

    public static void main(String[] args) {
//        m01();
//        m02();
//        m03();
        m04();
    }

    private static void m04() {
        int arr[] = {3, 9, -1, 10, 20};
        int temp = 0;
        boolean flag = false;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = true;
                }
            }
            System.out.println("第" + (i + 1) + "趟排序");
            System.out.println(Arrays.toString(arr));

            if (!flag) {
                break;
            } else {
                flag = false;
            }
        }
    }

    private static void m03() {
        int arr[] = {3, 9, -1, 10, 20};
        int temp = 0;
        boolean flag = false;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            System.out.println("第" + (i + 1) + "趟排序");
            System.out.println(Arrays.toString(arr));

            if (!flag) {
                break;
            } else {
                flag = false;
            }
        }
    }

    private static void m02() {
        int arr[] = {3, 9, -1, 10, 20};
        int temp = 0;
        boolean flag = false;  //标识   表示是否进行过交换
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            System.out.println("第" + (i + 1) + "趟排序");
            System.out.println(Arrays.toString(arr));

            if (!flag) {
                break;
            } else {
                flag = false;
            }
        }
    }

    private static void m01() {
        int arr[] = {3, 9, -1, 10, 20};
        int temp = 0;
        boolean flag = false; //标识变量，表示是否进行过交换
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            System.out.println("第" + (i + 1) + "趟排序");
            System.out.println(Arrays.toString(arr));

            if (!flag) {  //说明在一趟排序中  一次交换都没发生
                break;
            } else {
                flag = false;   //重置flag ，让代码能够进行下一趟排序
            }
        }
    }
}
